package com.warren.biz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.warren.annotation.ControllerEndpoint;
import com.warren.auth.entity.JwtUser;
import com.warren.biz.entity.Health;
import com.warren.biz.service.HealthService;
import com.warren.handler.BusinessException;
import com.warren.response.Result;
import com.warren.biz.vo.HealthVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author warren
 * @since 2021-04-20
 */
@RestController
@RequestMapping("/biz/health")
@Api(value = "健康模块接口",tags = "健康模块接口")
public class HealthController {

    @Resource
    private HealthService healthService;

    @ControllerEndpoint(exceptionMessage = "健康上报失败",operation = "健康上报")
    @ApiOperation(value = "健康上报",notes = "用户健康上报")
    @PostMapping("/report")
    public Result report( @RequestBody HealthVO healthVO)throws BusinessException{
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser userInfo = (JwtUser) authentication.getPrincipal();
        healthVO.setUserId(userInfo.getId());
        healthService.report(healthVO);
        return Result.ok();
    }

    @ApiOperation(value = "健康记录",notes = "用户健康上报历史记录")
    @PostMapping("/history")
    public Result history(@RequestParam(defaultValue = "1") Integer current,
                          @RequestParam(defaultValue = "6") Integer size,
                          @RequestBody HealthVO healthVO){
        System.out.println(healthVO);
        Page<Health> page = new Page<>(current,size);
        QueryWrapper<Health> wrapper = getWrapper(healthVO);
        IPage<Health> healthIPage = healthService.page(page,wrapper);

        long total = healthIPage.getTotal();
        List<Health> records = healthIPage.getRecords();

        return Result.ok().data("total",total).data("records",records);
    }

    @ApiOperation(value = "是否报备",notes = "今日是否报备")
    @GetMapping("/isReport")
    public Result isReport(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser userInfo = (JwtUser) authentication.getPrincipal();
        System.out.println(userInfo);
        Health report = healthService.isReport(userInfo.getId());
        return Result.ok().data("report",report);
    }

    private QueryWrapper<Health> getWrapper(HealthVO healthVO){
        QueryWrapper<Health> qw = new QueryWrapper<>();
        if (healthVO != null){
            if (!ObjectUtils.isEmpty(healthVO.getUserId())){
                qw.eq("user_id",healthVO.getUserId());
            }
        }
        return qw;
    }
}

